from flask import Flask
from flask import request
import config
from dingtalk.dingtalk import Dingtalk
from prometheus.prometheus import Prometheus


app = Flask(__name__)


@app.route("/", methods=["POST"])
def index():
    data = request.get_json(force=True)
    print(data)
    send(data)
    return '', 204


def send(data):
    alerts = data.get('alerts', None)
    if not alerts:
        return None
    for alert in alerts:
        prome = Prometheus(alert)
        prome.markddown_data()
        dingtalk = Dingtalk(config.webhook_url, config.mobiles, config.secret, config.msg_type)
        dingtalk._data(prome.format_data)
        dingtalk.send()


app.run(host=config.bind, port=config.port)